package parser
{
	import drawable.Point;
	
	import ndef.parser.Parser;
	
	public class Calculate extends Parser
	{
		
		private var varnames:Array = ["x"];
		private var _min_limit:int = -100;
		private var _max_limit:int = 100;
		private var _step:Number = .1;
		private var _result:Array;
		
		private var _expression:Array;
		
		public function Calculate()
		{
			super(this.varnames);
		}
		
		public function set max_limit( m:int ):void{
			this._max_limit = m;
		}
		public function set min_limit( m:int ):void{
			this._min_limit = m;
		}
		public function set step( s:int ):void{
			this._step = s;
		}
		
		public function get result():Array{
			return this._result;
		}
		
		public function calcExpression( exp:String ):Boolean{
			_expression = this.parse( exp );
			_result = [];
			if( this.success ){
				_expression = this.simplify( _expression );
				var i:Number = this._min_limit
				while( i <= this._max_limit ){
					_result.push( this.item_calc(i) );			
					i += _step;
				}
				return true;
				
			}else{
				trace(this.errormsg);
				return false;
			}	
		}
		
		private function item_calc( x:Number ):drawable.Point{
			return new Point(x,  Math.min(this._max_limit,  Math.max(this._min_limit, this.eval( _expression, [x] ) ) ) );
		}
		
		
	}
}